package jmine.tec.web.wicket.mock;

import java.io.Serializable;

import jmine.tec.web.wicket.dao.WebTestEntityDAO;
import jmine.tec.web.wicket.domain.WebTestEntity;
import bancosys.tec.persist.Persistable;
import bancosys.tec.persist.dao.BaseDAO;
import bancosys.tec.persist.dao.BaseDAOFactory;
import bancosys.tec.persist.dao.BeanNotFoundException;
import bancosys.tec.persist.dao.SimpleDAO;
import bancosys.tec.security.impl.dao.SecurityClientDAO;
import bancosys.tec.security.impl.dao.UserAccessDataDAO;
import bancosys.tec.security.impl.domain.SecurityClient;
import bancosys.tec.security.impl.domain.UserAccessData;

/**
 * Mock da {@link BaseDAOFactory}.
 * 
 * @author Rafael Volpato
 * @date Oct 4, 2011
 */
public class MockBaseDAOFactory extends BaseDAOFactory {

    /**
     * {@inheritDoc}
     */
    @Override
    @SuppressWarnings("unchecked")
    public <B extends Persistable> BaseDAO<B> getGenericDAO(Class<? extends B> type) {
        if (type.equals(WebTestEntity.class)) {
            return (BaseDAO<B>) new WebTestEntityDAO();
        } else {
            return new SimpleDAO<B>(type);
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public <Bo extends Persistable, DAO extends BaseDAO<Bo>> DAO getDAOByClass(Class<DAO> daoClass) {
        if (daoClass.equals(SecurityClientDAO.class)) {
            return daoClass.cast(new MockSecurityClientDAO());
        } else if (daoClass.equals(UserAccessDataDAO.class)) {
            return daoClass.cast(new MockUserAccessDataDAO());
        }

        try {
            return daoClass.newInstance();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * DAO da entidade SecurityClient
     * 
     * @author Rafael Volpato
     * @date Oct 4, 2011
     */
    private class MockSecurityClientDAO extends SecurityClientDAO {
        /**
         * {@inheritDoc}
         */
        @Override
        public SecurityClient findByPk(Serializable pk) throws BeanNotFoundException {
            SecurityClient securityClient = new SecurityClient();
            securityClient.setName("jmine-tec-web-wicket test app");
            return securityClient;
        }
    }

    /**
     * DAO da entidade UserAccessData
     * 
     * @author Rafael Volpato
     * @date Oct 4, 2011
     */
    private class MockUserAccessDataDAO extends UserAccessDataDAO {
        /**
         * {@inheritDoc}
         */
        @Override
        public UserAccessData findLastLoginByUsername(String username, String clientname) {
            return null;
        }
    }
}
